/**
 * OSAmI: Open Source Ambient Intelligence
 * Copyright (C) 2010 UJF.
 * Contact: osami-general@osami-commons.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 */

package org.osami.gateway.mock;

import static org.osami.gateway.sensor.data.SenseNode.TOPIC_PREFIX;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import org.osami.gateway.sensor.data.SenseData;
import org.osami.gateway.sensor.data.AbstractSenseData;
import org.osami.gateway.storage.ISenseDataStorage;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;
import org.osgi.util.measurement.Measurement;

/**
 * The SenseDataProducer component produce a lot of Mock SenseData.
 */
public class SenseDataProducer {

    private String senseId;

    private String sense_type;

    private String sense_unit;

    private int min;

    private int max;

    private ISenseDataStorage dataStore;

    private EventAdmin eventAdmin;

    private long period;

    private volatile boolean running = false;

    private void start() {
        //System.out.println("start " + this);
        Runnable producer = new Producer(max - min);
        running = true;
        new Thread(producer).start();
    }

    private void stop() {
        //System.out.println("stop " + this);
        running = false;
    }

    private class Producer implements Runnable {

        Map<String, Object> properties;

        Random r = new Random();

        private int delta;

        public Producer(int delta) {
            this.delta = delta;
        }

        public void run() {
            try {
                while (running) {

                    double value = r.nextInt(delta) + min;
                    SenseDataImpl senseData = new SenseDataImpl(value, senseId, new Measurement(value, 0.5, null, System.currentTimeMillis()));
                    senseData.setType(sense_type);
                    senseData.setUnit(sense_unit);

                    if (System.getenv("OSAMI_STRIPPED_GATEWAY") != null) {
                        //System.out.println("storing " + this);
                        dataStore.store(senseData);
                    }
                    else {
                        properties = new HashMap<String, Object>();
                        String topic = TOPIC_PREFIX + "sensor/" + sense_type;
                        properties.put(SenseData.DEFAULT_DATA_KEY, senseData);
                        //System.out.println("posting " + this);
                        eventAdmin.postEvent(new Event(topic, properties));
                    }

                    Thread.sleep(period);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private class SenseDataImpl extends AbstractSenseData {

        public SenseDataImpl(double value, String id, Measurement measurement) {
            super(value, id, measurement);
        }

        public String getLocation() {
            return "living-room";
        }

        public void setType(String type) {
            this.type = type;
        }

        public void setUnit(String unit) {
            this.unit = unit;
        }
    }
}
